############################################################################################
#                               名称: 基于Docker的Java开发环境
#                                   最后修改时间: 23.5.26
#                                         版本: v3
#                         镜像标记(imageTag): ubuntu/openjdk-17:v3
#                                       OS: ubuntu
#                                      OS版本: 23.04
#                                       建议修改内容:
#                                   createUserName 新用户名
#                                   createPassword 新用户密码
#                                  defRootPassword root用户密码
#                                   容器内缓存文件夹: /package
#                                          其他:
#                               自动配置国内软件源不需要手动修改
#                                       构建时需联网
# 适用于Java软件部署或测试, 目前已测试: Spring, MyBatis, CoralDatabase, CoralAPI, Tomcat等项目
#                           MySQL建议使用network链接MySQL容器
#               Spring和Tomcat需要开放8080端口，run时加上 -p 8080:8080 参数
#                   Shell默认配置oh-my-zsh,powerlevel10k,语法高亮,代码提示
#                                   默认使用新用户登陆
#                               默认工作路径为新用户的home路径
############################################################################################

############################## START ##############################
## 拉取 image
FROM ubuntu:23.04
## Dockerfile 作者信息
LABEL maintainer="hsdnmzz@163.com"
## 指定变量
# 软件包管理(非必要不要修改)
ARG pack="apt-get"
# 软件包安装(非必要不要修改)
ARG installc="${pack} install"
# 软件包更新(非必要不要修改)
ARG updatec="${pack} update"
# 新建用户名
ARG createUserName="javac"
# 新建用户名密码
ARG createPassword="123456789"
# root密码
ARG defRootPassword="123456789"
# sudo配置文件位置
ARG sudoSource="/etc/sudoers"
# apt软件源位置
ARG sourceListPath="/etc/apt/sources.list"
# Java默认位置
ARG JavaPath="/lib/jvm/jdk-17.0.7"
## 配置国内软件源
# 备份软件源
RUN mv ${sourceListPath} ${sourceListPath}.backup
# 写入新软件源
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ lunar main restricted universe multiverse" >> ${sourceListPath} \
    && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ lunar-updates main restricted universe multiverse" >> ${sourceListPath} \
    && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ lunar-backports main restricted universe multiverse" >> ${sourceListPath} \
    && echo "deb http://ports.ubuntu.com/ubuntu-ports/ lunar-security main restricted universe multiverse" >> ${sourceListPath}
# 为软件源文件给予权限
RUN chmod 777 ${sourceListPath}
## 创建缓存目录
RUN mkdir -p /package/out /lib/jvm /home/${createUserName}
## 安装必要的包
RUN ${updatec} && ${installc} vim net-tools sudo zsh git wget axel make gcc g++ openssh-server apt-transport-https ca-certificates curl software-properties-common -y
## 创建用户
# 创建并指定用户主文件夹, 修改新用户密码, 给予新用户sudo权限
RUN useradd -d /home/${createUserName} -s /bin/zsh ${createUserName} \
    && echo "${createUserName}:${createPassword}" | chpasswd \
    && echo "${createUserName} ALL=(ALL:ALL) ALL" >> ${sudoSource}
# 修改root密码
RUN echo "root:${defRootPassword}" | chpasswd
## 下载包
# 下载 Java JDK 版本 17, 下载 Tomcat 版本 10
RUN axel -a -n 32 https://download.oracle.com/java/17/latest/jdk-17_linux-aarch64_bin.tar.gz -o /package \
    && axel -a -n 32 https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.9/bin/apache-tomcat-10.1.9.tar.gz -o /package
# 给予新创建用户权限, 给予新用户访问权限
RUN chown ${createUserName} /package/* \
    && chown javac /home/${createUserName}
## 手动安装包
# 解压 Java JDK 安装包, 解压 Tomcat 安装包
RUN tar -zxvf /package/jdk-17_linux-aarch64_bin.tar.gz -C /package/out \
    && tar -zxvf /package/apache-tomcat-10.1.9.tar.gz -C /package/out
# 剪切到目录
RUN mv /package/out/jdk-17.0.7 /lib/jvm \
    && mv /package/out/apache-tomcat-10.1.9 /usr/local
## 安装 oh-my-zsh
# 指定新用户运行代码
USER ${createUserName}
# 删除.zshrc
RUN rm -rf /home/${createUserName}/.zshrc
# 拉取oh-my-zsh, 拉取zsh-autosuggestions, 拉取zsh-syntax-highlighting, 拉取powerlevel10k
RUN git clone https://gitee.com/mirrors/oh-my-zsh.git ~/.oh-my-zsh \
    && git clone https://gitee.com/hailin_cool/zsh-autosuggestions.git ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions \
    && git clone https://gitee.com/Annihilater/zsh-syntax-highlighting.git ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting \
    && git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ~/.oh-my-zsh/custom/themes/powerlevel10k
# 写入.p10k.zsh
RUN echo "'builtin' 'local' '-a' 'p10k_config_opts'" >> ~/.p10k.zsh \
    && echo "[[ ! -o 'aliases'         ]] || p10k_config_opts+=('aliases')" >> ~/.p10k.zsh \
    && echo "[[ ! -o 'sh_glob'         ]] || p10k_config_opts+=('sh_glob')" >> ~/.p10k.zsh \
    && echo "[[ ! -o 'no_brace_expand' ]] || p10k_config_opts+=('no_brace_expand')" >> ~/.p10k.zsh \
    && echo "'builtin' 'setopt' 'no_aliases' 'no_sh_glob' 'brace_expand'" >> ~/.p10k.zsh \
    && echo "() {" >> ~/.p10k.zsh \
    && echo "  emulate -L zsh -o extended_glob" >> ~/.p10k.zsh \
    && echo "  unset -m '(POWERLEVEL9K_*|DEFAULT_USER)~POWERLEVEL9K_GITSTATUS_DIR'" >> ~/.p10k.zsh \
    && echo "  [[ \$ZSH_VERSION == (5.<1->*|<6->.*) ]] || return" >> ~/.p10k.zsh \
    && echo "  local grey='242'" >> ~/.p10k.zsh \
    && echo "  local red='1'" >> ~/.p10k.zsh \
    && echo "  local yellow='3'" >> ~/.p10k.zsh \
    && echo "  local blue='4'" >> ~/.p10k.zsh \
    && echo "  local magenta='5'" >> ~/.p10k.zsh \
    && echo "  local cyan='6'" >> ~/.p10k.zsh \
    && echo "  local white='7'" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(" >> ~/.p10k.zsh \
    && echo "    dir" >> ~/.p10k.zsh \
    && echo "    vcs" >> ~/.p10k.zsh \
    && echo "    newline" >> ~/.p10k.zsh \
    && echo "    prompt_char" >> ~/.p10k.zsh \
    && echo "  )" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(" >> ~/.p10k.zsh \
    && echo "    command_execution_time" >> ~/.p10k.zsh \
    && echo "    virtualenv" >> ~/.p10k.zsh \
    && echo "    context" >> ~/.p10k.zsh \
    && echo "    time" >> ~/.p10k.zsh \
    && echo "    newline" >> ~/.p10k.zsh \
    && echo "  )" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_BACKGROUND=" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_{LEFT,RIGHT}_{LEFT,RIGHT}_WHITESPACE=" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_{LEFT,RIGHT}_SUBSEGMENT_SEPARATOR=' '" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_{LEFT,RIGHT}_SEGMENT_SEPARATOR=" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VISUAL_IDENTIFIER_EXPANSION=" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_PROMPT_ADD_NEWLINE=false" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_PROMPT_CHAR_OK_{VIINS,VICMD,VIVIS}_FOREGROUND=\$magenta" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_PROMPT_CHAR_ERROR_{VIINS,VICMD,VIVIS}_FOREGROUND=\$red" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_PROMPT_CHAR_{OK,ERROR}_VIINS_CONTENT_EXPANSION='~\$'" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_PROMPT_CHAR_{OK,ERROR}_VICMD_CONTENT_EXPANSION='\$~'" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_PROMPT_CHAR_{OK,ERROR}_VIVIS_CONTENT_EXPANSION='\$~'" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_PROMPT_CHAR_OVERWRITE_STATE=false" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VIRTUALENV_FOREGROUND=\$grey" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VIRTUALENV_SHOW_PYTHON_VERSION=false" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VIRTUALENV_{LEFT,RIGHT}_DELIMITER=" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_DIR_FOREGROUND=\$blue" >> ~/.p10k.zsh \
    && echo '  typeset -g POWERLEVEL9K_CONTEXT_ROOT_TEMPLATE="%F{\$white}%n%f%F{\$grey}@%m%f"' >> ~/.p10k.zsh \
    && echo '  typeset -g POWERLEVEL9K_CONTEXT_TEMPLATE="%F{\$grey}%n@%m%f"' >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_CONTEXT_{DEFAULT,SUDO}_CONTENT_EXPANSION=" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD=5" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION=0" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_COMMAND_EXECUTION_TIME_FORMAT='d h m s'" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_COMMAND_EXECUTION_TIME_FOREGROUND=\$yellow" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_FOREGROUND=\$grey" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_LOADING_TEXT=" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_MAX_SYNC_LATENCY_SECONDS=0" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_{INCOMING,OUTGOING}_CHANGESFORMAT_FOREGROUND=\$cyan" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_GIT_HOOKS=(vcs-detect-changes git-untracked git-aheadbehind)" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_BRANCH_ICON=" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_COMMIT_ICON='@'" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_{STAGED,UNSTAGED,UNTRACKED}_ICON=" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_DIRTY_ICON='*'" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_INCOMING_CHANGES_ICON=':⇣'" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_OUTGOING_CHANGES_ICON=':⇡'" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_{COMMITS_AHEAD,COMMITS_BEHIND}_MAX_NUM=1" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_VCS_CONTENT_EXPANSION='\${\${\${P9K_CONTENT/⇣* :⇡/⇣⇡}// }//:/ }'" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_TIME_FOREGROUND=\$grey" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_TIME_FORMAT='%D{%I:%M:%S %p}'" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_TIME_UPDATE_ON_COMMAND=false" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_TRANSIENT_PROMPT=always" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_INSTANT_PROMPT=verbose" >> ~/.p10k.zsh \
    && echo "  typeset -g POWERLEVEL9K_DISABLE_HOT_RELOAD=true" >> ~/.p10k.zsh \
    && echo "  (( ! \$+functions[p10k] )) || p10k reload" >> ~/.p10k.zsh \
    && echo "}" >> ~/.p10k.zsh \
    && echo "typeset -g POWERLEVEL9K_CONFIG_FILE=\${\${(%):-%x}:a}" >> ~/.p10k.zsh \
    && echo "(( \${#p10k_config_opts} )) && setopt \${p10k_config_opts[@]}" >> ~/.p10k.zsh \
    && echo "'builtin' 'unset' 'p10k_config_opts'" >> ~/.p10k.zsh
## 写入.zshrc
RUN echo 'if [[ -r "\${XDG_CACHE_HOME:-\$HOME/.cache}/p10k-instant-prompt-\${(%):-%n}.zsh" ]]; then' >> /home/${createUserName}/.zshrc \
    && echo '  source "\${XDG_CACHE_HOME:-\$HOME/.cache}/p10k-instant-prompt-\${(%):-%n}.zsh"' >> /home/${createUserName}/.zshrc \
    && echo "fi" >> /home/${createUserName}/.zshrc \
    && echo "export ZSH=\$HOME/.oh-my-zsh" >> /home/${createUserName}/.zshrc \
    && echo 'ZSH_THEME="powerlevel10k/powerlevel10k"' >> /home/${createUserName}/.zshrc \
    && echo "plugins=(git z sublime zsh-autosuggestions zsh-syntax-highlighting sudo)" >> /home/${createUserName}/.zshrc \
    && echo "source \$ZSH/oh-my-zsh.sh" >> /home/${createUserName}/.zshrc \
    && echo "[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh" >> /home/${createUserName}/.zshrc
## 指定root用户运行
USER root
## 配置环境变量
# 配置JAVA_HOME
ENV JAVA_HOME ${JavaPath}
# 配置CLASSPATH
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
# 配置PATH
ENV PATH $PATH:$JAVA_HOME/bin/:/usr/local/apache-tomcat-10.1.9/bin
## 修改默认 Shell
RUN chsh -s /bin/zsh
## 指定新用户
USER ${createUserName}
## 指定工作路径
WORKDIR /home/${createUserName}

RUN echo "User: ${createUserName}" >> doc.txt \
    && echo "${createUserName} Password: ${createPassword}" >> doc.txt \
    && echo "RootPassWord: ${defRootPassword}" >> doc.txt \
    && echo "JAVA_HOME: ${JAVA_HOME}" >> doc.txt \
    && echo "CLASSPATH: ${CLASSPATH}" \
    && echo "PATH: ${PATH}" >> doc.txt \
    && echo "source.list Backup: ${sourceListPath}.backup"
##############################  END  ##############################